"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes"));

var _parseInt2 = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/parse-int"));

/**
 * 版本号比较(只适用于版本号为数字,支持: 0.0.0,不支持: 0.0.0-alpha)
 *
 * @since 1.1.0
 * @category 实用函数
 * @param {string} v1 版本号1.
 * @param {string} v2 版本号2.
 * @param {object} config 配置参数.
 * @param {string} config.rule 比较规则(大于-gt,等于-et,小于-lt) 默认: gt
 *
 * @returns { boolean } 比较结果.
 *
 * @example
 *
 * z.compareVersion('0.0.0','0.0.1',{rule:'lt'})
 * // => true
 *
 * z.compareVersion('0.0.0','0.0.1',{rule:'et'})
 * // => false
 *
 * z.compareVersion('0.0.0','0.0.1',{rule:'gt'})
 * // => false
 *
 */
function compareVersion(v1, v2) {
  var _context;

  var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
      _ref$rule = _ref.rule,
      rule = _ref$rule === void 0 ? 'gt' : _ref$rule;

  if (typeof v1 !== 'string' || typeof v2 !== 'string') {
    throw new Error('版本号类型错误');
  }

  if (typeof rule !== 'string') {
    throw new Error('比较规则类型错误');
  }

  if (!(0, _includes.default)(_context = ['gt', 'et', 'lt']).call(_context, rule)) {
    throw new Error('比较规则传值错误');
  }

  var splitV1 = v1.split('.');
  var splitV2 = v2.split('.');
  var len = Math.max(splitV1.length, splitV2.length);

  while (splitV1.length < len) {
    splitV1.push('0');
  }

  while (splitV2.length < len) {
    splitV2.push('0');
  }

  if (rule === 'et') {
    return splitV1.every(function (v, i) {
      return (0, _parseInt2.default)(v, 10) === (0, _parseInt2.default)(splitV2[i], 10);
    });
  }

  for (var i = 0; i < len; i++) {
    var wholeV1 = (0, _parseInt2.default)(splitV1[i], 10);
    var wholeV2 = (0, _parseInt2.default)(splitV2[i], 10);

    if (rule === 'gt') {
      if (wholeV1 > wholeV2) return true;
      if (wholeV1 < wholeV2) return false;
    } else {
      if (wholeV1 > wholeV2) return false;
      if (wholeV1 < wholeV2) return true;
    }
  }

  return false;
}

module.exports = compareVersion;